package com.jbm.jbmsupplier.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.address.audiovideo.VideoCamer;
import com.jbm.jbmsupplier.activity.address.view.ShowImageViews;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by 家帮忙 on 2017/4/21.
 */

public class Gridviewlayout extends RecyclerView.Adapter<Gridviewlayout.MyViewHolders> {
    Context context;
    ArrayList<HashMap<String,String>> array;

    public Gridviewlayout(Context context, ArrayList<HashMap<String,String>> array) {
        this.context = context;
        this.array = array;
    }

    @Override
    public MyViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Gridviewlayout.MyViewHolders(View.inflate(context, R.layout.gridviewlayout_lz,null));
    }

    @Override
    public int getItemCount() {



        return array.size();
    }

    @Override
    public void onBindViewHolder(final MyViewHolders holder, final int position) {

        if (array.get(position).get("type").equals("pic")){
            holder.grid.setTag(position);
            Picasso.with(context).load(array.get(Integer.parseInt(holder.grid.getTag()+"")).get("pic")).resize(135,135).into(holder.grid);
        }else  if (array.get(position).get("type").equals("audio")){
            holder.grid.setTag(position);
            Picasso.with(context).load(R.mipmap.yuyinbofang).resize(135,135).into(holder.grid);
        }else  if (array.get(position).get("type").equals("video")){
            holder. grid.setTag(position);
            holder.videobofang.setVisibility(View.VISIBLE);
           Picasso.with(context).load(new File(array.get(Integer.parseInt(holder.grid.getTag()+"")).get("pic"))).resize(135,135).into(holder.grid);
        }
        holder.videobofang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, VideoCamer.class).putExtra("url",array.get(Integer.parseInt(holder.grid.getTag()+"")).get("pic")));
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (array.get(position).get("type").equals("pic")) {
                    context.startActivity(new Intent(context, ShowImageViews.class).putExtra("image", array.get(Integer.parseInt(holder.grid.getTag() + "")).get("pic")));
                }else if (array.get(position).get("type").equals("audio")){
                    playMusic(array.get(Integer.parseInt(holder.grid.getTag() + "")).get("pic"));
                }
            }
        });





    }
    private void playMusic(String name) {
        try {
            MediaPlayer mMediaPlayer = new MediaPlayer();
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(name);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {


                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
      class MyViewHolders extends RecyclerView.ViewHolder{
        ImageView grid,videobofang;
        public MyViewHolders(View itemView) {
            super(itemView);
            grid=(ImageView) itemView.findViewById(R.id.gridimage);
         videobofang=(ImageView) itemView.findViewById(R.id.videobofang);
        }
    }

}
